package org.point.controller;

import java.util.HashMap;
import java.util.Map;

import javax.ejb.FinderException;
import javax.enterprise.inject.Model;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.point.common.controller.BaseController;
import org.point.common.util.DoPage;
import org.point.domain.CreditsBehavior;
import org.point.domain.CreditsType;
import org.point.service.CreditsBehaviorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;

/**
 * 
 * @ClassName: CreditsBehaviorController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author Kenny_Z' szqiuk@gmail.com
 * @date 2016年10月23日 下午9:18:06
 *
 */
@Controller
@RequestMapping("/creditsB/")
public class CreditsBehaviorController extends BaseController {

	@Autowired
	private CreditsBehaviorService service;

	/**
	 * 
	 * @Title: cblist @Description: TODO(查询积分行为对象) @param @param
	 *         status @param @param response 设定文件 @return void 返回类型 @throws
	 */
	@RequestMapping(value = "cblist.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void cblist(@RequestParam(value = "currentPage") Integer currentPage,
			@RequestParam(value = "status", required = false) String status,
			@RequestParam(value = "keyWord", required = false) String keyWord, HttpServletResponse response) {

		JSONObject jsonObj = new JSONObject();
		DoPage<CreditsBehavior> page = new DoPage<CreditsBehavior>();
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if(strOperateUtil.isNullForString(keyWord)){
				map.put("keyWord", "%"+keyWord+"%");
			}
			if(strOperateUtil.isNullForString(status)){
				map.put("status", status);
			}
			if (currentPage != null) {
				page.setCurrentPage(currentPage);
				page.setMap(map);
				map.put("firstRecord", page.getFirstRecord());
				map.put("pageSize", page.getPageSize());
				page.setMap(map);
				DoPage<CreditsBehavior> records = service.searchCBForPage(page);
				jsonObj.put("page", records);
				jsonObj.put("resultMsg", 1);
			} else {
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", 0);
			e.printStackTrace();
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
	}

	/**
	 * 
	 * @Title: cbinsert @Description: TODO(新增积分行为对象) @param @param
	 *         record @param @param response 设定文件 @return void 返回类型 @throws
	 */
	@RequestMapping(value = "cbinsert.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void cbinsert(@Valid CreditsBehavior record, HttpServletResponse response) {

		JSONObject jsonObj = new JSONObject();
		Integer result = 0;
		String cbCode = getNextNo("CB");
		try {
			if (record != null) {
				// 新增积分行为记录
				Integer resutl = service.insertCreditsBehavior(record, cbCode);
				jsonObj.put("resultMsg", resutl);
			} else {
				// 积分行为参数为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", 0);
			throw new RuntimeException("新增积分行为", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
	}

	/**
	 * 
	 * @Title: delCB @Description: TODO(批量删除兑换规则分类) @param @param record
	 *         设定文件 @return void 返回类型 @throws
	 */
	@RequestMapping(value = "delCB.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void delCB(@RequestParam(value = "ids") String ids, HttpServletResponse response) {
		// 测试
		JSONObject jsonObj = new JSONObject();
		Integer result = 0;
		Map<String, Object> map = new HashMap<String, Object>();
		String newIDs = ids.substring(9, ids.length() - 1);
		try {
			map.put("newIDs", newIDs);
			result = service.deleteCBids(map);
			// result = service.deleteCT();
			if (result > 0) {
				// 兑换规则分类新增成功
				jsonObj.put("resultMsg", 1);
			} else {
				// 兑换规则分类参数为null， 请检查参数！
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("兑换规则新增错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}

	}

	/**
	 * 
	 * @Title: getCreditsType @Description:
	 *         TODO(根据积分规则分类ID，查询详细信息) @param @param creditsTypeId 设定文件 @return
	 *         void 返回类型 @throws
	 */
	@RequestMapping(value = "getCB.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void getCreditsBehavior(@RequestParam(value = "id") String id, HttpServletResponse response) {

		JSONObject jsonObj = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		Integer result = 0;

		try {
			if (strOperateUtil.isNullForString(id)) {
				map.put("id", Integer.parseInt(id));
				CreditsBehavior record = service.getCBByCBID(map);
				jsonObj.put("record", record);
				// 查询积分规则详细信息，成功
				jsonObj.put("resultMsg", 1);
			} else {
				// 积分规则分类ID为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("查询积分规则分类详细信息！错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}

	}

	/**
	 * 
	 * @Title: editstatus @Description: TODO(修改积分行为对象) @param @param
	 *         status @param @param behaviorId @param @param response
	 *         设定文件 @return void 返回类型 @throws
	 */
	@RequestMapping(value = "editstatus.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void editstatus(@RequestParam("status") String status, @RequestParam("behaviorId") String behaviorId,
			HttpServletResponse response) {

		JSONObject jsonObj = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		Integer result = 0;
		try {
			map.put("status", status);
			map.put("behaviorId", behaviorId);
			result = service.editCreditsBehavior(map);
			jsonObj.put("resultMsg", result);
		} catch (Exception e) {
			jsonObj.put("resultMsg", 0);
			throw new RuntimeException("修改积分行为对象状态", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}

	}

}
